/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include "SDL.h"

#ifdef _WIN32
    #include "windows.h"
#endif

#include "glfont2.h"
#include "IRender.h"
#include "TextureLoader.h"
#include "Camera.h"
#include "FontManager.h"

#include "../Commands.h"

/**
======================
OpenGL implementation of our Renderer
======================
*/
class GLRender : public IRender
{
public:
    GLRender(void);

    /** Init the backend */
    int Init();
    /** Clean up */
	void Shutdown();

    /** Set display name */
    void SetTitle(const std::string &title ) { m_title = title; };

    /** Draw an image */
	void DrawImage( Image *im, int x, int y);
    /** Draw an image at a set size, if repeat the the image repeats, else it stretchs */
    void DrawImage( Image *im, int x, int y, float width, float height, bool repeat=true);
    /** Draw an image which contains subimages */
    void DrawSubImage( Image *im, int x, int y, int row, int col);

    /** Draw a String */
    void DrawString( int x, int y, const char *s,...);
    /** Set a font type */
    void SetFont( const std::string &font ) { m_fontManager.SetFontType( font ); };
    /** Draw an empty rect */
	void DrawRect( int x, int y, int w, int h );
    /** Draw an empty circle */
    void DrawCircle( float radius, int x, int y);
    /** Draw a line */
    void DrawLine( const Vector2f &p1, const Vector2f &p2 );
    /** Fill in a rect */
	void FillRect( int x, int y, int w, int h );
	/** Fill in a rotated rect */
	void FillRectRotate(int x, int y, int w, int h, double rotation);

    /** Fill in a circle */
    void FillCircle( float radius, int x, int y);

    /** get a chars height */
    int GetTextHeight(char c) { 
        return m_fontManager.GetHeight();
    };
    
    /** Get a chars width */
    int GetTextWidth(char c) { 
        return m_fontManager.GetWidth();  
    };

    /** Set the current color */
	void  SetColor( float r, float g, float b, float a=1);
    void  SetColor(Color c);
    /** Get the current color */
	Color GetColor();

    /** Apply a color effect */
    void  SetColorEffect( float r, float g, float b, float a=1);
    void  SetColorEffect(Color c);
    /** Get the color effect color */
    Color GetColorEffect();

    /** Setup for this frames render */
	void PreRender();
    /** Clean up */
	void PostRender();

    /** Enable fog */
    void EnableFog();
    /** Disable fog */
    void DisableFog();
    /** Set the fog density */
    void SetFogDensity(float density);
    /** Set the fog color */
    void ChangeFogColor(float r, float g, float b, float a=1);
    void ChangeFogColor(Color c);
    /** Get the fog color */
    Color GetFogColor();

    /*----------------------------------------------------
        Camera
    ----------------------------------------------------*/
    ICamera* GetCamera() { return &m_camera; };
    void SetCamera( ICamera* c ) { /*m_camera = static_cast<Camera>(c))*;*/ };

private:
    /** Setup SDL */
    Rect SetupSDL(const std::string &title);
    /** Initialize OpenGL */
    void SetupOpenGL(int width, int height);
    /** Print driver info */
    void PrintDriverInfo();
    // background surface
    SDL_Surface*    screen;
    // our main  camera
    Camera          m_camera;
    // font renderer
    FontManager     m_fontManager;
    
    Color           m_color;    // current colors
    Color           m_fogColor;
    Color           m_cEffect;  // color effect
    float           m_fogDensity;
   
    // screen dimensions
    int     m_width,
            m_height;

    // screen title
    std::string m_title;

    /** Take a screeny */
    void CaptureScreen(const std::string &file="");
    /**
     * Screen shot function
     */
    class ScreenShot_f : public ICommand
    {
    public:
        ScreenShot_f() { renderer = NULL; };
        virtual ~ScreenShot_f() {};
        /** Create an ss */
        void Exec(std::string &s) {
            if ( renderer )
                renderer->CaptureScreen( s );
        };

        GLRender* renderer;
    }; friend ScreenShot_f;
    ScreenShot_f screen_f;

public:
    virtual ~GLRender(void);
};
